
export namespace BrowserUtils {
    export function getUrlFromCanvas(canvas: HTMLCanvasElement) {
        try {
            return canvas.toDataURL('image/png')
        } catch (error) {
            return ''
        }
    }

    export async function openFile<T>(text = true): Promise<T> {
        return new Promise((resolve, reject) => {
            const input = document.createElement('input')
            input.style.display = 'none'
            input.type = 'file'
            if (text) {
                input.accept = 'application/javascript,application/json'
            }
            document.body.appendChild(input)
            input.onchange = () => {
                const file = input.files?.[0]
                if (!file) {
                    input.remove()
                    reject('no file')
                    return
                }
                if (!text) {
                    input.remove()
                    resolve(file as T)
                    return
                }
                const reader = new FileReader()
                reader.onload=()=>resolve(reader.result as T)
                reader.readAsText(file)
                input.remove()
            }
            input.click()
        })

    }

}